package com.aop.example.aspect;

import com.aop.example.annotation.LogAnnotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.hibernate.validator.constraints.Mod11Check;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

import static cn.hutool.core.lang.Console.log;

@Slf4j
@Aspect     //切面注解标记
@Component  //ioc注入
public class LogAspect {
    /**
     * 切面对象类
     */
    //寻找切点注解
    @Pointcut("@annotation(com.aop.example.annotation.LogAnnotation)")
    public void logPointCut(){

    }

    //打印日志
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //日志输出
        String name = point.getSignature().getName();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        if(annotation != null){
            String value = annotation.value();
            log("【日志调用】当前操作:" + value + "，调用了" + name + "方法，返回值：" + point.proceed());
        }
        return point.proceed();
    }
}
